package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.CommentEntity;
import com.zw.yzk.kabu.data.network.entity.CosplayDetailsEntity;
import com.zw.yzk.kabu.data.network.entity.Entity;
import com.zw.yzk.kabu.data.network.task.CollectTask;
import com.zw.yzk.kabu.data.network.task.CommentTask;
import com.zw.yzk.kabu.data.network.task.CosDetailsTask;
import com.zw.yzk.kabu.data.network.task.FollowTask;
import com.zw.yzk.kabu.data.network.task.GetCommentTask;
import com.zw.yzk.kabu.data.network.task.PraiseTask;
import com.zw.yzk.kabu.data.network.task.VoteTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToastManager;
import com.zw.yzk.kabu.mapper.CosDetailsCommentMapper;
import com.zw.yzk.kabu.mapper.CosDetailsImageMapper;
import com.zw.yzk.kabu.view.view.CosDetailsView;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/2/24.
 */

public class CosplayDetailsPresenter implements Presenter {

    private UseCase getCosDetails;
    private UseCase commentCosplay;
    private UseCase collectCosplay;
    private UseCase praiseCosplay;
    private UseCase getCommentList;
    private UseCase follow;
    private  UseCase vote;
    private AppCompatActivity activity;
    private CosDetailsView cosDetailsView;
    private CosDetailsCommentMapper cosCommentMapper;
    private CosDetailsImageMapper cosImageMapper;
    private int totalComment;
    private int publisherId;

    @Inject
    CosplayDetailsPresenter(@Named("cosplayDetailsUseCase") UseCase getCosDetails,
                            @Named("commentUseCase") UseCase commentCosplay,
                            @Named("collectUseCase") UseCase collectCosplay,
                            @Named("praiseUseCase") UseCase praiseCosplay,
                            @Named("getCommmentUseCase") UseCase getCommentList,
                            @Named("followUseCase") UseCase follow,
                            @Named("voteUseCase") UseCase vote,
                            AppCompatActivity activity,
                            CosDetailsCommentMapper cosCommentMapper,
                            CosDetailsImageMapper cosImageMapper) {
        this.getCosDetails = getCosDetails;
        this.commentCosplay = commentCosplay;
        this.collectCosplay = collectCosplay;
        this.praiseCosplay = praiseCosplay;
        this.getCommentList = getCommentList;
        this.follow = follow;
        this.vote = vote;
        this.activity = activity;
        this.cosCommentMapper = cosCommentMapper;
        this.cosImageMapper = cosImageMapper;
    }

    public void setView(CosDetailsView cosDetailsView) {
        this.cosDetailsView = cosDetailsView;
    }


    public void getCosDetails(int cosId) {
        getCosDetails.execute(new GetCosplayDetailsSubscriber(activity), new CosDetailsTask(cosId));
    }

    public void follow(boolean concern) {
        follow.execute(new FollowSubscriber(activity), new FollowTask(publisherId, concern ? 1 : 0));
    }

    public void commentCos(int cosplayId, String replayId, String content) {
        commentCosplay.execute(new CommentCosplaySubscriber(activity), new CommentTask(cosplayId, replayId, content));
    }

    public void praise(int cosplayId, boolean praise) {
        praiseCosplay.execute(new PraiseCosplaySubscriber(activity), new PraiseTask(cosplayId, praise ? 1 : 0));
    }

    public void collect(int cosplayId, boolean collect) {
        collectCosplay.execute(new CollectCosplaySubscriber(activity), new CollectTask(cosplayId, collect ? 1 : 0));
    }

    public void getCommentList(int start, int cosplayId, int lastId) {
        getCommentList.execute(new GetCommentListSubscriber(activity), new GetCommentTask(start, totalComment, cosplayId, lastId));
    }

    public void vote(int cosId) {
        vote.execute(new VoteSubscriber(activity), new VoteTask(cosId));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        getCosDetails.unSubscribe();
        commentCosplay.unSubscribe();
        collectCosplay.unSubscribe();
        praiseCosplay.unSubscribe();
        getCommentList.unSubscribe();
        follow.unSubscribe();
        vote.unSubscribe();
    }

    private class VoteSubscriber extends BaseSubscriber<Entity> {

        public VoteSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(Entity entity) {
            super.onNext(entity);
            cosDetailsView.voteSuccess();
            ToastManager.getInstance().showToast(activity, entity.msg);
        }

        @Override
        public String addErrorMessage() {
            return null;
        }
    }

    private class FollowSubscriber extends BaseSubscriber<Boolean> {

        public FollowSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(Boolean entity) {
            super.onNext(entity);
            ToastManager.getInstance().showToast(activity,
                    activity.getResources().getString(cosDetailsView.getFollowState() ? R.string.follow_success : R.string.cancel_success));
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            cosDetailsView.followFailed();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(cosDetailsView.getFollowState() ? R.string.follow_failed : R.string.cancel_failed);
        }
    }

    private class GetCommentListSubscriber extends BaseSubscriber<CommentEntity> {

        public GetCommentListSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_comment_failed);
        }

        @Override
        public void onNext(CommentEntity entity) {
            super.onNext(entity);
            cosDetailsView.addCommentList(cosCommentMapper.transfer(entity.result.comments));
            totalComment = entity.result.totalComment;
            cosDetailsView.setTotalCommentCount(totalComment);
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }
    }

    private class GetCosplayDetailsSubscriber extends BaseSubscriber<CosplayDetailsEntity> {

        public GetCosplayDetailsSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(CosplayDetailsEntity entity) {
            super.onNext(entity);
            cosDetailsView.setDate(entity.result.publishTime);
            cosDetailsView.setHead(entity.result.publisherHead);
            cosDetailsView.setNickName(entity.result.publisher);
            cosDetailsView.setCommentList(cosCommentMapper.transfer(entity.result.comments));
            cosDetailsView.setTopImage(entity.result.homeImage);
            cosDetailsView.setImageList(cosImageMapper.transfer(entity.result.details));
            cosDetailsView.setDescribe(entity.result.sign);
            if (entity.result.isCompetion == 1) {
                cosDetailsView.showVoteInfo(true, entity.result.voteInfo.isVoted == 1, entity.result.voteInfo.ranking, entity.result.voteInfo.ballot);
            } else {
                cosDetailsView.showVoteInfo(false, entity.result.voteInfo.isVoted == 1, entity.result.voteInfo.ranking, entity.result.voteInfo.ballot);
            }
            cosDetailsView.showActionLayout();
            cosDetailsView.setPraiseAndCollectStatus(entity.result.isPraise, entity.result.isCollected,
                    entity.result.follwState == 1 || entity.result.follwState == 3);
            //当前评论总数
            totalComment = entity.result.totalComment;
            //发布者Id
            publisherId = entity.result.publisherId;
            cosDetailsView.setPublisherId(publisherId);
            //显示顶部remark
            cosDetailsView.setTopContent(entity.result.topContent);
            //显示评论总数
            cosDetailsView.setTotalCommentCount(totalComment);
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_cos_details_failed);
        }
    }

    private class CommentCosplaySubscriber extends BaseSubscriber<Boolean> {

        public CommentCosplaySubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(Boolean aBoolean) {
            super.onNext(aBoolean);
            getCosDetails(cosDetailsView.getCosplayId());
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.comment_cosplay_failed);
        }
    }

    private class CollectCosplaySubscriber extends BaseSubscriber<Boolean> {

        public CollectCosplaySubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(cosDetailsView.getCollectState() ? R.string.collect_failed : R.string.cancel_failed);
        }

        @Override
        public void onNext(Boolean aBoolean) {
            super.onNext(aBoolean);
            ToastManager.getInstance().showToast(activity,
                    activity.getResources().getString(cosDetailsView.getCollectState() ? R.string.collect_success : R.string.cancel_success));
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            cosDetailsView.collectFailed();
        }
    }

    private class PraiseCosplaySubscriber extends BaseSubscriber<Boolean> {

        public PraiseCosplaySubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(cosDetailsView.getPraiseState() ? R.string.praise_failed : R.string.cancel_failed);
        }

        @Override
        public void onNext(Boolean aBoolean) {
            super.onNext(aBoolean);
            ToastManager.getInstance().showToast(activity,
                    activity.getResources().getString(cosDetailsView.getPraiseState() ? R.string.praise_success : R.string.cancel_success));
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            cosDetailsView.praiseFialed();
        }
    }
}
